home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / modal.exe / MODAL.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-05-23  |  8.4 KB  |  390 lines

  1. unit modal;
  2.  
  3. (*
  4.    This unit implements following objects
  5.  
  6.       T_CWINDOW       Makes it easy to create windows with a new window
  7.                       class without overriding GetClassName and
  8.                       GetWindowClass functions.  You specify the new class
  9.                       name in the call to the init constructor.  You can
  10.                       make all the class attribute changes in the init
  11.                       constructor also.  It is derived from TWindow object.
  12.  
  13.       T_MODALWINDOW   This object allows you to create modal windows which
  14.                       behave like a dialog box.  It will let you create
  15.                       dialog boxes of your own without having to create a
  16.                       dialog resource first.  You can easily create a window
  17.                       with any OWL controls you want, dynamically, and use
  18.                       it as a dialog box.
  19.  
  20.    This units also implements some related function to translate dialog
  21.    units into screen units etc.  It also contains TBGroupBox and TBStatic
  22.    to use BWCC specific featues.
  23.  
  24.    Copyright (c) 1993 by Yasser Asmi  (CIS 71543,2252)
  25.    All rights reserved.
  26.  
  27.    If you have any questions or comments, please contact me either at
  28.    CIS 71543,2252 or at the following address.  Thank you.
  29.  
  30.                                 Yasser Asmi
  31.                                 400 W. Union, #6
  32.                                 Edwardsville, IL 62025
  33. *)
  34.  
  35. {$A+,I-,R-,S-,V-,B-,G+,X+,W-}
  36.  
  37. interface
  38.  
  39. uses
  40.    wintypes, winprocs, win31, strings, objects, owindows, odialogs, bwcc;
  41.  
  42. type
  43.    p_cwindow = ^t_cwindow;
  44.    t_cwindow = object (twindow)
  45.       cwndclass : twndclass;
  46.       cclassname : pchar;
  47.  
  48.       constructor init (aparent : pwindowsobject;
  49.                         atitle : pchar;
  50.                         aclassname : pchar);
  51.       function getclassname : pchar; virtual;
  52.       procedure getwindowclass (var awndclass : twndclass); virtual;
  53.       destructor done; virtual;
  54.    end;
  55.  
  56.    p_modalwindow = ^t_modalwindow;
  57.    t_modalwindow = object (t_cwindow)
  58.       noclose : boolean;
  59.       complete : boolean;
  60.       retval : integer;
  61.  
  62.       constructor init (aparent : pwindowsobject;
  63.                         atitle : pchar;
  64.                         aclassname : pchar);
  65.       procedure setupwindow; virtual;
  66.       function canclose : boolean; virtual;
  67.       procedure b_ok (var msg : tmessage); virtual id_first + id_ok;
  68.       procedure b_cancel (var msg : tmessage); virtual id_first + id_cancel;
  69.       procedure wmclose (var msg : tmessage); virtual wm_first + wm_close;
  70.       procedure endwin (aretval : integer);
  71.       procedure disablekbhandler;
  72.       procedure beginmodal;
  73.       procedure endmodal;
  74.  
  75.    private
  76.       lastmodal : pwindowsobject;
  77.       lastfocus : hwnd;
  78.    end;
  79.  
  80.    pbgroupbox = ^tbgroupbox;
  81.    tbgroupbox = object (tgroupbox)
  82.       constructor init (aparent : pwindowsobject;
  83.                         anid : integer;
  84.                         atext : pchar;
  85.                         x, y, w, h : integer);
  86.       function getclassname : pchar; virtual;
  87.    end;
  88.  
  89.    pbstatic = ^tbstatic;
  90.    tbstatic = object (tstatic)
  91.       function getclassname : pchar; virtual;
  92.    end;
  93.  
  94.  
  95. function run_modal (p : p_modalwindow;
  96.                     calldone : boolean) : integer;  (* see proc *)
  97. procedure handlemessage;
  98. function getprogwin : pwindow;                      (* get program window *)
  99. function dux (x : integer) : integer;               (* dialog units to screen *)
  100. function duy (y : integer) : integer;
  101. procedure center_wa (var attr : twindowattr;        (* center window attr *)
  102.                      w, h : integer);
  103.  
  104.  
  105. implementation
  106.  
  107. const
  108.    curmodalp : pwindowsobject = nil;
  109.  
  110.  
  111. (*-- T_CWINDOW --*)
  112.  
  113. constructor t_cwindow.init;
  114.  
  115. begin
  116.    inherited init (aparent, atitle);
  117.    cclassname := strnew (aclassname);
  118.    inherited getwindowclass (cwndclass);
  119.    cwndclass.lpszclassname := cclassname;
  120. end;
  121.  
  122.  
  123. destructor t_cwindow.done;
  124.  
  125. begin
  126.    strdispose (cclassname);
  127.    inherited done;
  128. end;
  129.  
  130.  
  131. function t_cwindow.getclassname;
  132.  
  133. begin
  134.    getclassname := cclassname;
  135. end;
  136.  
  137.  
  138. procedure t_cwindow.getwindowclass;
  139.  
  140. begin
  141.    awndclass := cwndclass;
  142. end;
  143.  
  144.  
  145. (*-- T_MODALWINDOW --*)
  146.  
  147. constructor t_modalwindow.init;
  148.  
  149. begin
  150.    inherited init (aparent, atitle, aclassname);
  151.    enablekbhandler;
  152.    attr.style := ws_popup or ws_visible or ws_caption or ws_sysmenu;
  153.    attr.exstyle := ws_ex_dlgmodalframe;
  154.  
  155.    noclose := false;
  156.    complete := false;
  157. end;
  158.  
  159.  
  160. procedure t_modalwindow.setupwindow;
  161.  
  162. var
  163.    mh : hmenu;
  164.  
  165. begin
  166.    inherited setupwindow;
  167.  
  168.    mh := getsystemmenu (hwindow, false);
  169.    deletemenu (mh, 5, mf_byposition);
  170.    deletemenu (mh, 4, mf_byposition);
  171.    deletemenu (mh, 3, mf_byposition);
  172.    deletemenu (mh, 2, mf_byposition);
  173.    deletemenu (mh, 0, mf_byposition);
  174.    if noclose then
  175.       enablemenuitem (mh, sc_close, mf_disabled + mf_grayed);
  176.  
  177.    if childlist <> nil then
  178.       setfocus (childlist^.hwindow);
  179. end;
  180.  
  181.  
  182. function t_modalwindow.canclose;
  183.  
  184. begin
  185.    canclose := not noclose;
  186. end;
  187.  
  188.  
  189. procedure t_modalwindow.disablekbhandler;
  190.  
  191. begin
  192.    setflags (wb_kbhandler, false);
  193. end;
  194.  
  195.  
  196. procedure t_modalwindow.endwin;
  197.  
  198. begin
  199.    if canclose then
  200.    begin
  201.       retval := aretval;
  202.       complete := true;
  203.    end;
  204. end;
  205.  
  206.  
  207. procedure t_modalwindow.b_ok;
  208.  
  209. begin
  210.    endwin (id_ok);
  211. end;
  212.  
  213.  
  214. procedure t_modalwindow.b_cancel;
  215.  
  216. begin
  217.    endwin (id_cancel);
  218. end;
  219.  
  220.  
  221. procedure t_modalwindow.wmclose;
  222.  
  223. begin
  224.    b_cancel (msg);
  225. end;
  226.  
  227.  
  228. procedure t_modalwindow.beginmodal;
  229.  
  230. begin
  231.    lastfocus := getfocus;
  232.    if curmodalp = nil then
  233.       lastmodal := getprogwin
  234.    else
  235.       lastmodal := curmodalp;
  236.    enablewindow (parent^.hwindow, false);
  237.    application^.makewindow (@self);
  238.    curmodalp := @self;
  239. end;
  240.  
  241.  
  242. procedure t_modalwindow.endmodal;
  243.  
  244. (*
  245.    does not call the done destructor
  246. *)
  247.  
  248. begin
  249.    enablewindow (parent^.hwindow, true);
  250.    setfocus (lastfocus);
  251.    curmodalp := lastmodal;
  252. end;
  253.  
  254.  
  255. (*-- TBGROUPBOX --*)
  256.  
  257. constructor tbgroupbox.init;
  258.  
  259. begin
  260.    inherited init (aparent, anid, atext, x, y, w, h);
  261.    attr.style := (attr.style and not bs_groupbox) or bss_group;
  262. end;
  263.  
  264.  
  265. function tbgroupbox.getclassname;
  266.  
  267. begin
  268.    getclassname := shade_class;
  269. end;
  270.  
  271.  
  272. (*-- TBSTATIC --*)
  273.  
  274. function tbstatic.getclassname;
  275.  
  276. begin
  277.    getclassname := static_class;
  278. end;
  279.  
  280.  
  281. (*-- PROCS --*)
  282.  
  283. function run_modal (p : p_modalwindow;
  284.                     calldone : boolean) : integer;
  285.  
  286. (*
  287.    If calldone=true then, the done destructor is called from this
  288.    procedure after completion.  Otherwise, the caller must call the
  289.    done destructor.
  290. *)
  291.  
  292. begin
  293.    p^.beginmodal;
  294.    while not p^.complete do
  295.       handlemessage;
  296.    p^.endmodal;
  297.    run_modal := p^.retval;
  298.    if calldone then
  299.       p^.done;
  300. end;
  301.  
  302.  
  303. function getprogwin : pwindow;
  304.  
  305. (*
  306.    returns programs mainwindow or nil if application is nil
  307. *)
  308.  
  309. begin
  310.    if application = nil then
  311.       getprogwin := nil
  312.    else
  313.       getprogwin := pwindow (application^.mainwindow);
  314. end;
  315.  
  316.  
  317. procedure handlemessage;
  318.  
  319. (*
  320.   gets a message from the queue and processes it
  321.   if the message is WM_KILLFOCUS it is not processed
  322. *)
  323.  
  324. var
  325.    m : tmsg;
  326.  
  327. begin
  328.    if peekmessage (m, 0, 0, 0, pm_remove) then
  329.    begin
  330.       if not application^.processappmsg (m) then
  331.       begin
  332.          if m.message <> wm_killfocus then
  333.          begin
  334.             translatemessage (m);
  335.             dispatchmessage (m);
  336.          end
  337.          else
  338.             messagebeep (10);
  339.       end;
  340.    end;
  341. end;
  342.  
  343.  
  344. function dux (x : integer) : integer;
  345.  
  346. (*
  347.    converts horizontal dialog units into screen unit
  348. *)
  349.  
  350. begin
  351.    dux := (x * loword (getdialogbaseunits)) div 4;
  352. end;
  353.  
  354.  
  355. function duy (y : integer) : integer;
  356.  
  357. (*
  358.    converts vertical dialog units into screen unit
  359. *)
  360.  
  361. begin
  362.    duy := (y * hiword (getdialogbaseunits)) div 8;
  363. end;
  364.  
  365.  
  366. procedure center_wa (var attr : twindowattr;
  367.                      w, h : integer);
  368.  
  369. (*
  370.    Sets width and height fields in window attr to w and h
  371.    and the centers the window on screen.  Call it from the
  372.    init constructor of a window.
  373. *)
  374.  
  375.  function cen (mw, w : integer) : integer;
  376.  
  377.  begin
  378.     cen := (mw div 2) - (w div 2)
  379.  end;
  380.  
  381. begin
  382.    attr.w := w;
  383.    attr.h := h;
  384.    attr.x := cen (getsystemmetrics (sm_cxscreen), w);
  385.    attr.y := cen (getsystemmetrics (sm_cyscreen), h);
  386. end;
  387.  
  388.  
  389. end.
  390.